package util;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletResponse;

public class CharSetFilter implements Filter{
	private FilterConfig fc;
	
	@Override
	public void init(FilterConfig filterconfig) throws ServletException {
		// TODO Auto-generated method stub
		fc = filterconfig;
	}

	@Override
	public void doFilter(ServletRequest servletrequest,
			ServletResponse servletresponse, FilterChain filterchain)
			throws IOException, ServletException {
		// TODO Auto-generated method stub
		String charSet = fc.getInitParameter("charSet");
		if(null == charSet || "".equals(charSet)){
			charSet = "utf-8";
		}
		servletrequest.setCharacterEncoding(charSet);
		servletresponse.setCharacterEncoding(charSet);
		servletresponse.setContentType("text/html;charset="+charSet);
		filterchain.doFilter(servletrequest, servletresponse);
	}

	@Override
	public void destroy() {
		// TODO Auto-generated method stub
		
	}

}
